<template>
  <div>
    <div style="display: flex; justify-content: space-between"></div>
    <div>
      <h1>教师信息总览</h1>
    </div>
    <el-divider><i class="el-icon-mobile-phone"></i></el-divider>
    <div class="teaMain">
      <el-table size="small" stripe border :data="teachers" fit>
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="id" label="工号"> </el-table-column>
        <el-table-column prop="clazz" label="任教班级"> </el-table-column>
        <el-table-column prop="room" label="教研室"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="showTea(scope.$index, scope.row)">
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="thandleDelete(scope.$index, scope.row)"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog title="编辑教师" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-tag>教师姓名</el-tag>
        <el-input
          size="small"
          v-model="updateTea.name"
          class="updateTeaInput"
        ></el-input>
        <el-tag>任教班级</el-tag>
        <el-input
          size="small"
          v-model="updateTea.clazz"
          class="updateTeaInput"
        ></el-input>
        <el-tag>教师办公室</el-tag>
        <el-input
          size="small"
          v-model="updateTea.room"
          class="updateTeaInput"
        ></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdateTea"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <el-pagination background layout="prev, pager, next" :total="5">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "teacherSearch",
  data() {
    return {
      tea: {
        name: "",
      },
      teachers: [],
      dialogVisible: false,
      updateTea: {
        name: "",
      },
    };
  },
  mounted() {
    this.initTeacher();
  },
  methods: {
    showTea(index, data) {
      this.updateTea = data;
      this.dialogVisible = true;
    },
    doUpdateTea() {
      this.putRequest("/teacher/update", this.updateTea).then((resp) => {
        if (resp) {
          this.initTeacher();
          this.dialogVisible = false;
        }
      });
    },
    initTeacher() {
      this.getRequest("/teacher/").then((resp) => {
        if (resp) {
          this.teachers = resp;
        }
      });
    },
    thandleDelete(index, data) {
      this.$confirm(
        "此操作将永久删除[" + data.name + "]老师, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.deleteRequest("/teacher/" + data.id).then((resp) => {
            if (resp) {
              this.initTeacher();
            }
          });
          // this.$message({
          //   type: 'success',
          //   message: '删除成功!'
          // });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.updateTeaInput {
  width: 100px;
  margin-left: 8px;
}
.teaMain {
  margin-top: 30px;
}
</style>